package org.freewill.example.action;

import java.util.List;

import org.freewill.db.QueryHelper;
import org.freewill.example.model.Article;
import org.freewill.web.ActionSupper;
import org.freewill.web.Mapping;

public class BlogAction extends ActionSupper {

	@Mapping(value = "/blog/list")
	public void list() {
		if(page.isCache()){
			List<Article> list = QueryHelper.list(Article.class,"created DESC");
			page.assign("articles", list);
			page.cache();
		}
		page.display();
	}

	@Mapping(value = "/blog/show")
	public void show() throws IllegalArgumentException, IllegalAccessException {
		QueryHelper<Article> query = new QueryHelper<Article>(Article.class);
		if(params.get("id",Long.class)!=null){
			query.andWhere("id", params.get("id", Long.class));
		}
		if(params.get("title")!=null){
			query.andWhere("title", params.get("title"));
		}
		
		Article article = query.first();
		if(article ==null){
			article = new Article();
			article.setTitle("白醋");
			article.setBody("喝不");
			article.setViews(0);
		}
		article.setViews(article.getViews()+1);
		article.save();
		page.assign("art", article);
		page.display();
	}
}